package cn.elead.chaos.sso.server.web.system.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.ApiAssert;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.shiro.util.ShiroUtils;
import cn.elead.chaos.sso.core.util.SsoUtils;
import cn.elead.chaos.sso.server.constants.UserConstants;
import cn.elead.chaos.sso.server.web.system.domain.SysMenu;
import cn.elead.chaos.sso.server.web.system.domain.SysRole;
import cn.elead.chaos.sso.server.web.system.domain.Ztree;
import cn.elead.chaos.sso.server.web.system.service.ISysMenuService;

/**
 * 菜单信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {
	private String prefix = "system/menu";

	@Autowired
	private ISysMenuService menuService;

	@RequiresPermissions("system:menu:view")
	@GetMapping()
	public String menu() {
		return prefix + "/menu";
	}

	@ResponseBody
	@UnifiedReturn
	@PostMapping("/list")
	@RequiresPermissions("system:menu:list")
	public List<SysMenu> list(SysMenu menu) {
		String userId = SsoUtils.getUserId();
		List<SysMenu> menuList = menuService.selectMenuList(menu, userId);
		return menuList;
	}

	/**
	 * 删除菜单
	 */
	@ResponseBody
	@Transactional
	@GetMapping("/remove/{menuId}")
	@RequiresPermissions("system:menu:remove")
	@Log(title = "菜单管理", businessType = BusinessType.DELETE)
	public void remove(@PathVariable("menuId") Long menuId) {
		if (menuService.selectCountMenuByParentId(menuId) > 0) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("存在子菜单,不允许删除"));
		}
		if (menuService.selectCountRoleMenuByMenuId(menuId) > 0) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("菜单已分配,不允许删除"));
		}
		menuService.deleteMenuById(menuId);
	}

	/**
	 * 新增
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Long parentId, ModelMap mmap) {
		SysMenu menu = null;
		if (0L != parentId) {
			menu = menuService.selectMenuById(parentId);
		} else {
			menu = new SysMenu();
			menu.setMenuId(0L);
			menu.setMenuName("主目录");
		}
		mmap.put("menu", menu);
		return prefix + "/add";
	}

	/**
	 * 新增保存菜单
	 */
	@ResponseBody
	@PostMapping("/add")
	@Transactional
	@RequiresPermissions("system:menu:add")
	@Log(title = "菜单管理", businessType = BusinessType.INSERT)
	public SysMenu addSave(@Validated SysMenu menu) {
		if (UserConstants.MENU_NAME_NOT_UNIQUE == menuService.checkMenuNameUnique(menu)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在"));
		}
		menu.setCreateBy(SsoUtils.getUserName());
		menuService.insertMenu(menu);
		return menu;
	}

	/**
	 * 修改菜单
	 */
	@GetMapping("/edit/{menuId}")
	public String edit(@PathVariable("menuId") Long menuId, ModelMap mmap) {
		mmap.put("menu", menuService.selectMenuById(menuId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存菜单
	 */
	@ResponseBody
	@PostMapping("/edit")
	@Transactional
	@RequiresPermissions("system:menu:edit")
	@Log(title = "菜单管理", businessType = BusinessType.UPDATE)
	public SysMenu editSave(@Validated SysMenu menu) {
		if (UserConstants.MENU_NAME_NOT_UNIQUE == menuService.checkMenuNameUnique(menu)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在"));
		}
		menu.setUpdateBy(ShiroUtils.getLoginName());
		menuService.updateMenu(menu);
		return menu;
	}

	/**
	 * 选择菜单图标
	 */
	@GetMapping("/icon")
	public String icon() {
		return prefix + "/icon";
	}

	/**
	 * 校验菜单名称
	 */
	@ResponseBody
	@PostMapping("/checkMenuNameUnique")
	public int checkMenuNameUnique(SysMenu menu) {
		return menuService.checkMenuNameUnique(menu);
	}

	/**
	 * 加载角色菜单列表树
	 */
	@ResponseBody
	@UnifiedReturn
	@GetMapping("/roleMenuTreeData")
	public List<Ztree> roleMenuTreeData(SysRole role) {
		String userId = SsoUtils.getUserId();
		List<Ztree> ztrees = menuService.roleMenuTreeData(role, userId);
		return ztrees;
	}

	/**
	 * 加载所有菜单列表树
	 */
	@ResponseBody
	@UnifiedReturn
	@GetMapping("/menuTreeData")
	public List<Ztree> menuTreeData() {
		String userId = SsoUtils.getUserId();
		List<Ztree> ztrees = menuService.menuTreeData(userId);
		return ztrees;
	}

	/**
	 * 选择菜单树
	 */
	@GetMapping("/selectMenuTree/{menuId}")
	public String selectMenuTree(@PathVariable("menuId") Long menuId, ModelMap mmap) {
		mmap.put("menu", menuService.selectMenuById(menuId));
		return prefix + "/tree";
	}
}